package com.bestcem.xm.ticket.dao.converter;

import com.bestcem.xm.ticket.entity.mongo.TicketTemplateV2;
import com.bestcem.xm.ticket.entity.mysql.TicketTemplateV2DO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/23 13:55
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TicketTemplateV2Converter {

    @Nullable
    public static TicketTemplateV2 doToDto(TicketTemplateV2DO ticketTemplateV2DO) {
        if (Objects.isNull(ticketTemplateV2DO)) {
            return null;
        }

        TicketTemplateV2 ticketTemplateV2 = new TicketTemplateV2();
        ticketTemplateV2.setId(ticketTemplateV2DO.getTemplateId());
        ticketTemplateV2.setOrgId(ticketTemplateV2DO.getOrgId());
        ticketTemplateV2.setTicketConfigId(ticketTemplateV2DO.getTicketConfigId());
        ticketTemplateV2.setName(ticketTemplateV2DO.getName());
        ticketTemplateV2.setType(ticketTemplateV2DO.getType());
        ticketTemplateV2.setSeq(ticketTemplateV2DO.getSeq());
        ticketTemplateV2.setCreator(ticketTemplateV2DO.getCreator());
        ticketTemplateV2.setFieldList(CommonConverter
                .stringToList(ticketTemplateV2DO.getFieldList(), TicketTemplateV2.FieldInfo.class));
        ticketTemplateV2.setUsed(ticketTemplateV2DO.getUsed());
        ticketTemplateV2.setSample(ticketTemplateV2DO.getSample());

        CommonConverter.setEntity(ticketTemplateV2, ticketTemplateV2DO);
        return ticketTemplateV2;
    }

    @Nonnull
    public static List<TicketTemplateV2> doListToDtoList(List<TicketTemplateV2DO> ticketTemplateV2DOList) {
        List<TicketTemplateV2> list = new ArrayList<>();
        if (Objects.isNull(ticketTemplateV2DOList)) {
            return list;
        }

        for (TicketTemplateV2DO ticketTemplateV2DO : ticketTemplateV2DOList) {
            TicketTemplateV2 ticketTemplateV2 = doToDto(ticketTemplateV2DO);
            list.add(ticketTemplateV2);
        }
        return list;
    }

    @Nullable
    public static TicketTemplateV2DO dtoToDo(TicketTemplateV2 ticketTemplateV2) {
        if (Objects.isNull(ticketTemplateV2)) {
            return null;
        }

        TicketTemplateV2DO ticketTemplateV2DO = new TicketTemplateV2DO();
        ticketTemplateV2DO.setTemplateId(ticketTemplateV2.getId());
        ticketTemplateV2DO.setOrgId(ticketTemplateV2.getOrgId());
        ticketTemplateV2DO.setTicketConfigId(ticketTemplateV2.getTicketConfigId());
        ticketTemplateV2DO.setName(ticketTemplateV2.getName());
        ticketTemplateV2DO.setType(ticketTemplateV2.getType());
        ticketTemplateV2DO.setSeq(ticketTemplateV2.getSeq());
        ticketTemplateV2DO.setCreator(ticketTemplateV2.getCreator());
        ticketTemplateV2DO.setFieldList(CommonConverter.listToString(ticketTemplateV2.getFieldList()));
        ticketTemplateV2DO.setUsed(ticketTemplateV2.getUsed());
        ticketTemplateV2DO.setUsed(ticketTemplateV2.getUsed());
        ticketTemplateV2DO.setSample(ticketTemplateV2.getSample());

        CommonConverter.setDo(ticketTemplateV2, ticketTemplateV2DO);
        return ticketTemplateV2DO;
    }
}
